package com.web.controller.system;

import com.domain.store.Company;
import com.domain.system.Dept;
import com.github.pagehelper.PageInfo;
import com.service.system.DeptService;
import com.service.system.impl.DeptServiceImpl;
import com.utils.BeanUtil;
import com.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@WebServlet("/system/dept")
public class DeptServlet extends BaseServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String operation = request.getParameter("operation");
        if ("list".equals(operation)){
            list(request,response);
        }else if ("toAdd".equals(operation)){
            toAdd(request,response);
        }else if ("save".equals(operation)){
            save(request,response);
        }else if ("delete".equals(operation)){
            delete(request,response);
        }else if ("edit".equals(operation)){
            edit(request,response);
        }else if ("toEdit".equals(operation)){
            toEdit(request,response);
        }
    }

    private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //封装数据
        Dept dept = BeanUtil.fillBean(request, Dept.class, "yyyy-MM-dd");
        //调用业务层代码
        deptService.update(dept);
        //重定向
        response.sendRedirect(request.getContextPath()+"/system/dept?operation=list");
    }

    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //根据id查询
        String id = request.getParameter("id");
        Dept dept = deptService.findById(id);
        //存入域中
        request.setAttribute("dept",dept);
        List<Dept> all = deptService.findAll();
        request.setAttribute("deptList",all);
        //转发
        request.getRequestDispatcher("/WEB-INF/pages/system/dept/update.jsp").forward(request,response);
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //得到参数id
        Dept dept = BeanUtil.fillBean(request, Dept.class);
        //调用业务层
        deptService.delete(dept);
        //重定向
        response.sendRedirect(request.getContextPath()+"/system/dept?operation=list");
    }

    private void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //封装数据
        Dept dept = BeanUtil.fillBean(request, Dept.class, "yyyy-MM-dd");
        //调用业务层代码
        deptService.save(dept);
        //重定向
        response.sendRedirect(request.getContextPath()+"/system/dept?operation=list");
    }

    private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //得到所有的部门信息并放入到deptList
        List<Dept> all = deptService.findAll();
        request.setAttribute("deptList",all);
        //转发至添加页面
        request.getRequestDispatcher("/WEB-INF/pages/system/dept/add.jsp").forward(request,response);
    }

    private void list(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //定义当前页和每页条数
        int page = 1;
        int size = 5;
        //得到参数page和size
        if (StringUtils.isNoneBlank(request.getParameter("page"))){
            page = Integer.parseInt(request.getParameter("page"));
        }
        if (StringUtils.isNoneBlank(request.getParameter("size"))){
            size = Integer.parseInt(request.getParameter("size"));
        }
        //调用业务层
        PageInfo all = deptService.findAll(page, size);
        //存至域中
        request.setAttribute("page",all);
        //转发至list.jsp页面
        request.getRequestDispatcher("/WEB-INF/pages/system/dept/list.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
